using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;

namespace LCPrinter
{
    public static class Prints
    {
        //打印图片（图片的字节流，打印张数，打印的名字）
        public static void PrintTexture(byte[] texture2DBytes, int numCopies, string printerName)
        {
            //字节流是否为空
            if (texture2DBytes == null)
            {
                //输出提示
                UnityEngine.Debug.LogWarning("LCPrinter: Texture is empty.");
                return;
            }
            //打印设置新建对象
            PrinterSettings printerSettings = new PrinterSettings();
            //打印名字为空或者等于空
            if (printerName == null || printerName.Equals(""))
            {
                printerName = printerSettings.PrinterName;
                UnityEngine.Debug.Log("LCPrinter: Printing to default printer (" + printerName + ").");
            }
            //储存时间段信息
            string str = string.Concat(new string[]
            {
                DateTime.Now.Year.ToString(),
                "-",
                DateTime.Now.Month.ToString(),
                "-",
                DateTime.Now.Day.ToString(),
                "-",
                DateTime.Now.Hour.ToString(),
                "-",
                DateTime.Now.Minute.ToString(),
                "-",
                DateTime.Now.Second.ToString(),
                "-",
                DateTime.Now.Millisecond.ToString()
            });
            //文本
            string text = (Application.persistentDataPath + "\\LCPrinterFiletmp_" + str + ".JPG").Replace("/", "\\");
            UnityEngine.Debug.Log("LCPrinter: Temporary Path - " + text);
            //字节流写入
            File.WriteAllBytes(text, texture2DBytes);
            Prints.PrintCMD(text, numCopies, printerName);
        }

        //通过纹理路径（路径，打印张数，输出的名字）
        public static void PrintTextureByPath(string path, int numCopies, string printerName)
        {
            //打印机设置
            PrinterSettings printerSettings = new PrinterSettings();
            //判断是否为空
            if (printerName == null || printerName.Equals(""))
            {
                //设置打印机的名字为默认
                printerName = printerSettings.PrinterName;
            }
            //UnityEngine.Debug.Log("打印机的名字： (" + printerName + ").");
            path =path.Replace("/", "\\");
            UnityEngine.Debug.Log("打印文件地址： (" + path + ").");

            Prints.PrintCMD(path, numCopies, printerName);
        }

        //CMD
        public static void PrintCMD(string path, int numCopies, string printerName)
        {
            //对进程的控制
            Process process = new Process();
            try
            {
                for (int i = 0; i < numCopies; i++)
                {
                    process.StartInfo.FileName = "rundll32";
                    process.StartInfo.Arguments = string.Concat(new string[]
                    {
                        "C:\\WINDOWS\\system32\\shimgvw.dll,ImageView_PrintTo \"",
                        path,
                        "\" \"",
                        printerName,
                        "\""
                    });
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                    process.StartInfo.UseShellExecute = true;
                    process.Start();
                }
            }
            catch (Exception arg)
            {
                UnityEngine.Debug.LogWarning("LCPrinter: " + arg);
            }
            finally
            {
                process.Close();
                UnityEngine.Debug.Log("LCPrinter: Texture printing."+ path);
                // 修改消息框的样式标志
                MessageBox(IntPtr.Zero, "正在打印。。。请等待。。。", "打印照片", MB_TOPMOST | MB_SYSTEMMODAL);
            }
        }
        // 使用P/Invoke调用Windows API中的MessageBox函数
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);

        // 消息框的样式标志
        private const uint MB_TOPMOST = 0x40000;
        private const uint MB_SYSTEMMODAL = 0x1000;
    }
}
